import wx

from pyDemin import *


#Classe d'affichage du jeu
class DeminWindowSolo(wx.Panel):
    def __init__(self, parent, ID,nb,etat):
        wx.Panel.__init__(self,parent,ID)
        self.SetSize(parent.GetClientSize())
        self.init_buffer()
        self.Bind(wx.EVT_PAINT,self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN,self.OnLeft)
        self.Bind(wx.EVT_RIGHT_DOWN,self.OnRight)
        self.n=nb
        self.wm=30
        self.hm=30
        self.game=pyDemin(30,30,self.n)
        self.etat=etat
        
        self.p=parent
        
    def OnLeft(self,evt):
        x,y = evt.GetPosition()
        a=x/20
        b=y/20
        self.game.decouvre(a,b)
        mm=self.refresh()
        self.p.SetStatusText("il reste %d mines"%(self.n-mm))

    def OnRight(self,evt):
        x,y = evt.GetPosition()
        a=x/20
        b=y/20
        self.game.aamine(a,b)
        mm=self.refresh()
        self.p.SetStatusText("il reste %d mines"%(self.n-mm))
                
    def OnPaint(self,evt):
        dc = wx.BufferedPaintDC(self,self.buffer)
    
    def init_buffer(self):
        w,h = self.GetClientSize()
        self.buffer = wx.EmptyBitmap(w,h)
        dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
        self.draw(dc)
        
	
    def draw(self,dc):
        dc.Clear()
        dc.SetBrush(wx.Brush("grey"))
        dc.SetPen(wx.Pen("black",1))
        for i in range(30):
            for j in range(30):
                dc.DrawRectangle(i*20,j*20,20,20)

    def refresh(self):
        dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
        dc.Clear()
        dc.SetPen(wx.Pen("black",1))
        t=True
        nb=0
        mm=0
        for i in range(30):
            for j in range(30):
                if t:
                    if self.game.dec[j*self.game.w+i]==0:
                        dc.SetBrush(wx.Brush("grey"))
                        dc.DrawRectangle(i*20,j*20,20,20)
                    else :
                        if self.game.dec[j*self.game.w+i]==-1:
                            dc.SetBrush(wx.Brush("red"))
                            dc.DrawRectangle(i*20,j*20,20,20)
                            mm+=1
                        else :
                            if self.game.mine[j*self.game.w+i]==-1:
                                dc.Clear()
                                dc.SetFont(wx.Font(50, wx.MODERN, wx.NORMAL, wx.NORMAL))
                                dc.SetPen(wx.Pen('black', 20))
                                dc.DrawText("P E R D U",120,250)
                                t=False
                            else:
                                dc.SetBrush(wx.Brush("gold"))
                                dc.DrawRectangle(i*20,j*20,20,20)
                                k=self.game.grille[j*self.game.w+i]
                                if k!=0 :
                                    dc.DrawText("%d"%k,i*20+5,j*20+2)
                                nb+=1
        if nb==30*30-self.n:
            dc.Clear()
            dc.SetFont(wx.Font(50, wx.MODERN, wx.NORMAL, wx.NORMAL))
            dc.SetPen(wx.Pen('black', 20))
            dc.DrawText("G A G N E",120,250)
        return mm
            
      


          
            
#Classe pour le jeu
class GameSoloFrame(wx.Frame):

    def __init__( self, parent, ID, title, pos=wx.DefaultPosition,size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):	
        wx.Frame.__init__(self, parent, ID, title, pos, size, style)
        
        self.SetAutoLayout(True)
        self.GameWindow = DeminWindowSolo(self,-1,50,0)
        self.GameWindow.init_buffer()
        
        menubar=wx.MenuBar()
       
        menu =wx.Menu(style=wx.MENU_TEAROFF)
        menubar.Append(menu,"&File")

        menu1=wx.Menu(style=wx.MENU_TEAROFF)
        menu1.Append(3,"Facile")
        menu1.Append(4,"Moyen")
        menu1.Append(5,"Difficile")
        
        menu.AppendMenu(wx.ID_OPEN,"New",menu1)
        menu.AppendSeparator()
        menu.Append(wx.ID_EXIT,"&Quit","Quit")
        
        self.SetMenuBar(menubar)
        self.Bind (wx.EVT_MENU,self.fnew , id=wx.ID_OPEN)
        self.Bind (wx.EVT_MENU,self.fquit , id=wx.ID_EXIT)
        self.Bind (wx.EVT_MENU,self.fnewf , id=3)
        self.Bind (wx.EVT_MENU,self.fnewm , id=4)
        self.Bind (wx.EVT_MENU,self.fnewd , id=5)
        
        self.statusBar = self.CreateStatusBar() 
        self.CreateStatusBar()
        self.SetStatusText("Cliquez sur la grille pour jouer")

    def fquit(self,evt) :
        self.Destroy()

    def fnew(self,evt) :
        self.GameWindow = DeminWindowSolo(self,-1,60,0)
        self.GameWindow.init_buffer()

    def fnewf(self,evt) :
        self.GameWindow = DeminWindowSolo(self,-1,45,0)
        self.GameWindow.init_buffer()

    def fnewm(self,evt) :
        self.GameWindow = DeminWindowSolo(self,-1,85,0)
        self.GameWindow.init_buffer()

    def fnewd(self,evt) :
        self.GameWindow = DeminWindowSolo(self,-1,120,0)
        self.GameWindow.init_buffer()
